package com.example.elmmerchants.Message_Setting;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;
import com.google.android.material.tabs.TabLayout;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Message_Setting_Page extends AppCompatActivity {

    Switch is_open_online_contact_switch, is_open_finish_auto_send_message_switch;
    TextView merchant_custom_name_text_view, message_text_view, busy_state_auto_message_text_view;
    RelativeLayout try_to_listen_ringtone, set_contact_name;
    LinearLayout set_busy_state_text, set_computer_reply;
    ImageButton edit_finish_auto_send_message;

    String email, merchant_custom_name, message, busy_state_auto_message;
    int how_many_volume, is_open_online_contact, is_open_finish_auto_send_message, is_open_first_reminder, is_open_one_min_after_reminder, is_open_three_min_after_reminder, is_open_computer_reply, is_open_supplementary_delivery_auto_reply, is_open_auto_reminder_order_reply;
    boolean is_finish_loading_book_order_data, is_finish_loading_computer_auto_reply_data;
    AlertDialog dialog, busy_state_dialog, finish_order_auto_send_message_dialog, contact_name_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_setting_page);

        email = getIntent().getStringExtra("email");
        merchant_custom_name = getIntent().getStringExtra("merchant_custom_name");
        message = getIntent().getStringExtra("message");
        busy_state_auto_message = getIntent().getStringExtra("busy_state_auto_message");
        is_open_online_contact = getIntent().getIntExtra("is_open_online_contact", 0);
        is_open_finish_auto_send_message = getIntent().getIntExtra("is_open_finish_auto_send_message", 0);

        is_open_online_contact_switch = findViewById(R.id.is_open_online_contact);
        merchant_custom_name_text_view = findViewById(R.id.merchant_custom_name);
        is_open_finish_auto_send_message_switch = findViewById(R.id.is_open_finish_auto_send_message);
        message_text_view = findViewById(R.id.message);
        busy_state_auto_message_text_view = findViewById(R.id.busy_state_auto_message);
        try_to_listen_ringtone = findViewById(R.id.try_to_listen_ringtone);
        set_busy_state_text = findViewById(R.id.set_busy_state_text);
        edit_finish_auto_send_message = findViewById(R.id.edit_finish_auto_send_message);
        set_contact_name = findViewById(R.id.set_contact_name);
        set_computer_reply = findViewById(R.id.set_computer_reply);

        if (is_open_online_contact == 1) {
            is_open_online_contact_switch.setChecked(true);
        }
        else {
            is_open_online_contact_switch.setChecked(false);
        }

        if (is_open_finish_auto_send_message == 1) {
            is_open_finish_auto_send_message_switch.setChecked(true);
        }
        else {
            is_open_finish_auto_send_message_switch.setChecked(false);
        }

        merchant_custom_name_text_view.setText(merchant_custom_name);
        message_text_view.setText(message);
        busy_state_auto_message_text_view.setText(busy_state_auto_message);

        is_open_online_contact_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_online_contact_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_online_contact = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_online_contact_switch.setChecked(true);
                            is_open_online_contact = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_online_contact = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_online_contact_switch.setChecked(false);
                            is_open_online_contact = 0;
                        });
                    });
                }
            }
        });

        is_open_finish_auto_send_message_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_finish_auto_send_message_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_finish_auto_send_message = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_finish_auto_send_message_switch.setChecked(true);
                            is_open_finish_auto_send_message = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_finish_auto_send_message = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_finish_auto_send_message_switch.setChecked(false);
                            is_open_finish_auto_send_message = 0;
                        });
                    });
                }
            }
        });

        try_to_listen_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();
                is_finish_loading_book_order_data = false;

                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "SELECT * FROM merchant_message_setting WHERE email = ?;";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setString(1, email);
                        ResultSet set = preparedStatement.executeQuery();
                        while (set.next()) {
                            is_open_first_reminder = set.getInt("is_open_first_reminder");
                            is_open_one_min_after_reminder = set.getInt("is_open_one_min_after_reminder");
                            is_open_three_min_after_reminder = set.getInt("is_open_three_min_after_reminder");
                        }

                        String sql2 = "SELECT * FROM merchant_ringtone_setting WHERE email = ?;";
                        PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, email);
                        ResultSet set2 = preparedStatement2.executeQuery();
                        while (set2.next()) {
                            how_many_volume = set2.getInt("volume");
                            is_finish_loading_book_order_data = true;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

                while (!is_finish_loading_book_order_data) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                dialog.dismiss();
                Intent intent = new Intent(Message_Setting_Page.this, Message_Ringtone_Page.class);
                intent.putExtra("email", email);
                intent.putExtra("is_open_first_reminder", is_open_first_reminder);
                intent.putExtra("is_open_one_min_after_reminder", is_open_one_min_after_reminder);
                intent.putExtra("is_open_three_min_after_reminder", is_open_three_min_after_reminder);
                intent.putExtra("how_many_volume", how_many_volume);
                startActivity(intent);
            }
        });

        set_busy_state_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.set_busy_state_text_reply_view, null);
                EditText set_busy_state_text = view1.findViewById(R.id.set_busy_state_text);
                set_busy_state_text.setText(busy_state_auto_message);
                TextView text_length = view1.findViewById(R.id.text_length);
                text_length.setText(String.valueOf(busy_state_auto_message.length()));
                set_busy_state_text.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        text_length.setText(String.valueOf(set_busy_state_text.getText().length()));
                    }
                });
                TextView cancel = view1.findViewById(R.id.cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        busy_state_dialog.dismiss();
                    }
                });
                TextView save = view1.findViewById(R.id.save);
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ExecutorService executorService = Executors.newSingleThreadExecutor();
                        executorService.execute(() -> {
                            try {
                                Connection_SQL userSQL = new Connection_SQL();
                                Connection connection = userSQL.connection();
                                String sql = "update `merchant_message_setting` set busy_state_auto_message = ? where email = ?";
                                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                                preparedStatement.setString(1, set_busy_state_text.getText().toString());
                                preparedStatement.setString(2, email);
                                preparedStatement.executeUpdate();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            runOnUiThread(() -> {
                                try {
                                    Thread.sleep(100);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                busy_state_auto_message_text_view.setText(set_busy_state_text.getText().toString());
                                busy_state_auto_message = set_busy_state_text.getText().toString();
                                busy_state_dialog.dismiss();
                            });
                        });
                    }
                });

                builder.setView(view1);
                builder.setCancelable(false);

                busy_state_dialog = builder.create();
                busy_state_dialog.show();
            }
        });

        edit_finish_auto_send_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.set_finish_order_auto_send_message, null);
                EditText set_busy_state_text = view1.findViewById(R.id.set_busy_state_text);
                set_busy_state_text.setText(message);
                TextView text_length = view1.findViewById(R.id.text_length);
                text_length.setText(String.valueOf(message.length()));
                set_busy_state_text.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        text_length.setText(String.valueOf(set_busy_state_text.getText().length()));
                    }
                });
                TextView cancel = view1.findViewById(R.id.cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish_order_auto_send_message_dialog.dismiss();
                    }
                });
                TextView save = view1.findViewById(R.id.save);
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ExecutorService executorService = Executors.newSingleThreadExecutor();
                        executorService.execute(() -> {
                            try {
                                Connection_SQL userSQL = new Connection_SQL();
                                Connection connection = userSQL.connection();
                                String sql = "update `merchant_message_setting` set finish_auto_send_what_message = ? where email = ?";
                                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                                preparedStatement.setString(1, set_busy_state_text.getText().toString());
                                preparedStatement.setString(2, email);
                                preparedStatement.executeUpdate();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            runOnUiThread(() -> {
                                try {
                                    Thread.sleep(100);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                message_text_view.setText(set_busy_state_text.getText().toString());
                                message = set_busy_state_text.getText().toString();
                                finish_order_auto_send_message_dialog.dismiss();
                            });
                        });
                    }
                });

                builder.setView(view1);
                builder.setCancelable(false);

                finish_order_auto_send_message_dialog = builder.create();
                finish_order_auto_send_message_dialog.show();
            }
        });

        set_contact_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.set_contact_name_view, null);
                EditText set_busy_state_text = view1.findViewById(R.id.set_busy_state_text);
                set_busy_state_text.setText(merchant_custom_name);
                TextView text_length = view1.findViewById(R.id.text_length);
                text_length.setText(String.valueOf(merchant_custom_name.length()));
                set_busy_state_text.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        text_length.setText(String.valueOf(set_busy_state_text.getText().length()));
                    }
                });
                TextView cancel = view1.findViewById(R.id.cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        contact_name_dialog.dismiss();
                    }
                });
                TextView save = view1.findViewById(R.id.save);
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ExecutorService executorService = Executors.newSingleThreadExecutor();
                        executorService.execute(() -> {
                            try {
                                Connection_SQL userSQL = new Connection_SQL();
                                Connection connection = userSQL.connection();
                                String sql = "update `merchant_message_setting` set merchant_contact_name = ? where email = ?";
                                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                                preparedStatement.setString(1, set_busy_state_text.getText().toString());
                                preparedStatement.setString(2, email);
                                preparedStatement.executeUpdate();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            runOnUiThread(() -> {
                                try {
                                    Thread.sleep(100);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                merchant_custom_name_text_view.setText(set_busy_state_text.getText().toString());
                                merchant_custom_name = set_busy_state_text.getText().toString();
                                contact_name_dialog.dismiss();
                            });
                        });
                    }
                });

                builder.setView(view1);
                builder.setCancelable(false);

                contact_name_dialog = builder.create();
                contact_name_dialog.show();
            }
        });

        set_computer_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();
                is_finish_loading_computer_auto_reply_data = false;

                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "SELECT * FROM merchant_message_setting WHERE email = ?;";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setString(1, email);
                        ResultSet set = preparedStatement.executeQuery();
                        while (set.next()) {
                            is_open_computer_reply = set.getInt("is_open_computer_reply");
                            is_open_supplementary_delivery_auto_reply = set.getInt("is_open_supplementary_delivery_auto_reply");
                            is_open_auto_reminder_order_reply = set.getInt("is_open_auto_reminder_order_reply");
                            is_finish_loading_computer_auto_reply_data = true;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

                while (!is_finish_loading_computer_auto_reply_data) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                dialog.dismiss();
                Intent intent = new Intent(Message_Setting_Page.this, Computer_Auto_Reply.class);
                intent.putExtra("email", email);
                intent.putExtra("is_open_computer_reply", is_open_computer_reply);
                intent.putExtra("is_open_supplementary_delivery_auto_reply", is_open_supplementary_delivery_auto_reply);
                intent.putExtra("is_open_auto_reminder_order_reply", is_open_auto_reminder_order_reply);
                startActivityForResult(intent, 1);
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}